package com.iotproject.blogservice.controller;


import com.iotproject.blogservice.client.UcenterClient;
import com.iotproject.blogservice.entity.MyBlog;
import com.iotproject.blogservice.entity.vo.MBlogEdit;
import com.iotproject.blogservice.entity.vo.MBlogQuery;
import com.iotproject.blogservice.service.MBlogService;
import com.iotproject.commonutils.R;
import com.iotproject.commonutils.forumvo.UcenterMemberForum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 博客 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-03-29
 */
@RestController
@RequestMapping("/blogservice/m-blog")
public class MBlogController {
    @Autowired
    MBlogService mBlogService;

    @Autowired
    UcenterClient ucenterClient;

    //插入博客
    @GetMapping("insertBlog")
    public R insertBlogType() {
        MyBlog myBlog1 = new MyBlog();
        myBlog1.setUserId("1637665299110801409");myBlog1.setNickname("mi²go");
        myBlog1.setTitle("文章标题");myBlog1.setDescription("文章简介");
        myBlog1.setContent("文章内容");myBlog1.setViewCount(190L);
        myBlog1.setBlogType("1640980172511023106");
        boolean save = mBlogService.save(myBlog1);
        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //查询博客列表
    @GetMapping("blogList")
    public R getBlogList() {
        QueryWrapper<MyBlog> mBlogQueryWrapper = new QueryWrapper<>();
        mBlogQueryWrapper.orderByDesc("gmt_create");
        List<MyBlog> list = mBlogService.list(mBlogQueryWrapper);
        //遍历列表返回其sign值
        List<String> signList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MyBlog myBlog = list.get(i);
            String userId = myBlog.getUserId();
            UcenterMemberForum userInfoForum = ucenterClient.getUserInfoForum(userId);
            String sign = userInfoForum.getSign();
            signList.add(sign);
        }
        return R.ok().data("items", list).data("len", list.size()).data("signs", signList);
    }

    //根据博客类别和相关信息查询
    @PostMapping("getListByMBlogQuery")
    public R getListByMBlogQuery(@RequestBody(required = false)MBlogQuery mBlogQuery) {
        QueryWrapper<MyBlog> mBlogQueryWrapper = new QueryWrapper<>();

        String searchArticleContent = mBlogQuery.getSearchArticleContent();
        String blogType = mBlogQuery.getBlogType();

        //封装查询条件(模糊查询 + 条件查询)
        if (!StringUtils.isEmpty(searchArticleContent)) {
            mBlogQueryWrapper.and(wrapper ->
                    wrapper.like("title", searchArticleContent).or().like("description", searchArticleContent)
            );
        }
        if (!blogType.equals("-1")) {//有类型
            mBlogQueryWrapper.and(wrapper -> wrapper.eq("blog_type", blogType));
        }
        mBlogQueryWrapper.orderByDesc("gmt_create");
        List<MyBlog> list = mBlogService.list(mBlogQueryWrapper);
        //遍历列表返回其sign值
        List<String> signList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            MyBlog myBlog = list.get(i);
            String userId = myBlog.getUserId();
            UcenterMemberForum userInfoForum = ucenterClient.getUserInfoForum(userId);
            String sign = userInfoForum.getSign();
            signList.add(sign);
        }
        return R.ok().data("items", list).data("len", list.size()).data("signs", signList);
    }

    //添加博客
    @PostMapping("editArticle")
    public R editArticle(@RequestBody MBlogEdit mBlogEdit) {
        MyBlog myBlog = new MyBlog();
        BeanUtils.copyProperties(mBlogEdit, myBlog);
        boolean save = mBlogService.save(myBlog);
        if (save) {
            return R.ok();
        }
            return R.error();
    }

    //根据博客id进行查询
    @GetMapping("getArticle/{id}")
    public R getArticle(@PathVariable("id") String id) {
        MyBlog myBlog = mBlogService.getById(id);
        //增加浏览数
        mBlogService.increaseViewCount(id);
        if (myBlog == null) {
            return R.error();
        } else {
            return R.ok().data("article", myBlog);
        }
    }

    //根据博客id进行修改
    @PostMapping("updateArticle")
    public R updateArticle(@RequestBody MyBlog myBlog) {
        boolean b = mBlogService.updateById(myBlog);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }
//    //讲师修改功能
//    @PostMapping("updateTeacher")
//    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
//        boolean b = teacherService.updateById(eduTeacher);
//        if (b) {
//            return R.ok();
//        } else {
//            return R.error();
//        }
//    }

    //根据用户id分页展示所有文章
    /**
     * 根据页码和大小分页查询
     * @param page 当前页，默认为1
     * @param size 当前每页显示行数，默认为5
     * @return 页信息的实体
     */
    @GetMapping("/userArticleList/{userId}/{page}/{size}")
    public R userArticleList(@PathVariable("userId") String userId, @PathVariable("page") Integer page, @PathVariable("size") Integer size) {
        Map<String, Object> maps = mBlogService.userArticleList(userId, page, size);
        return R.ok().data("maps", maps);
    }

}

